const Service = require('egg').Service;

class UserService extends Service {
  async addCart(params) {
    var {id,userId} = params
    var item = await this.ctx.model.Goods.find({_id:id})
    var isExist = await this.ctx.model.User.find({userId,"cartList._id":id.trim()})
    var msg;
    var code;
    if(!isExist.length){
        await this.ctx.model.User.updateOne({userId},{$push:{cartList:item[0]}})
        code=200
        msg ='添加购物车成功'
    }else{
        code=400
        msg ='购物车中已存在'
    }
    return {msg,code};
  }
  async getCart(query){
    var {userId} = query
    var data = await this.ctx.model.User.find({userId})
    var result = data[0].cartList
    return result
  }
}

module.exports = UserService;